package com.packt.webstore.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.packt.webstore.domain.Customer;
import com.packt.webstore.domain.repository.CustomerRepository;

@Service
public class CustomerServiceImpl {
	
	@Autowired
	private CustomerRepository customerRepository;


	public void processCustomer(String customerId, int quantity) {
  Customer customerById = customerRepository.getCustomerById(customerId);
		
		if(customerById.getNoOfOrdersMade() < quantity){
			throw new IllegalArgumentException("Out of Stock. Available Units in stock " +
		customerById.getNoOfOrdersMade());
		}
		
		customerById.setNoOfOrdersMade(customerById.getNoOfOrdersMade() - quantity);
	}

}
